{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table class=\"ee-notebook-buttons\" align=\"left\">\n",
    "    <td><a target=\"_blank\"  href=\"https://github.com/giswqs/earthengine-py-notebooks/tree/master/GetStarted/01_hello_world.ipynb\"><img width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /> View source on GitHub</a></td>\n",
    "    <td><a target=\"_blank\"  href=\"https://nbviewer.jupyter.org/github/giswqs/earthengine-py-notebooks/blob/master/GetStarted/01_hello_world.ipynb\"><img width=26px src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Jupyter_logo.svg/883px-Jupyter_logo.svg.png\" />Notebook Viewer</a></td>\n",
    "    <td><a target=\"_blank\"  href=\"https://colab.research.google.com/github/giswqs/earthengine-py-notebooks/blob/master/GetStarted/01_hello_world.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /> Run in Google Colab</a></td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Install Earth Engine API and geemap\n",
    "Install the [Earth Engine Python API](https://developers.google.com/earth-engine/python_install) and [geemap](https://github.com/giswqs/geemap). The **geemap** Python package is built upon the [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) and [folium](https://github.com/python-visualization/folium) packages and implements several methods for interacting with Earth Engine data layers, such as `Map.addLayer()`, `Map.setCenter()`, and `Map.centerObject()`.\n",
    "The following script checks if the geemap package has been installed. If not, it will install geemap, which automatically installs its [dependencies](https://github.com/giswqs/geemap#dependencies), including earthengine-api, folium, and ipyleaflet.\n",
    "\n",
    "**Important note**: A key difference between folium and ipyleaflet is that ipyleaflet is built upon ipywidgets and allows bidirectional communication between the front-end and the backend enabling the use of the map to capture user input, while folium is meant for displaying static data only ([source](https://blog.jupyter.org/interactive-gis-in-jupyter-with-ipyleaflet-52f9657fa7a)). Note that [Google Colab](https://colab.research.google.com/) currently does not support ipyleaflet ([source](https://github.com/googlecolab/colabtools/issues/60#issuecomment-596225619)). Therefore, if you are using geemap with Google Colab, you should use [`import geemap.eefolium`](https://github.com/giswqs/geemap/blob/master/geemap/eefolium.py). If you are using geemap with [binder](https://mybinder.org/) or a local Jupyter notebook server, you can use [`import geemap`](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py), which provides more functionalities for capturing user input (e.g., mouse-clicking and moving)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Installs geemap package\n",
    "import subprocess\n",
    "\n",
    "try:\n",
    "    import geemap\n",
    "except ImportError:\n",
    "    print('geemap package not installed. Installing ...')\n",
    "    subprocess.check_call([\"python\", '-m', 'pip', 'install', 'geemap'])\n",
    "\n",
    "# Checks whether this notebook is running on Google Colab\n",
    "try:\n",
    "    import google.colab\n",
    "    import geemap.eefolium as geemap\n",
    "except:\n",
    "    import geemap\n",
    "\n",
    "# Authenticates and initializes Earth Engine\n",
    "import ee\n",
    "\n",
    "try:\n",
    "    ee.Initialize()\n",
    "except Exception as e:\n",
    "    ee.Authenticate()\n",
    "    ee.Initialize()  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create an interactive map \n",
    "The default basemap is `Google Maps`. [Additional basemaps](https://github.com/giswqs/geemap/blob/master/geemap/basemaps.py) can be added using the `Map.add_basemap()` function. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "159d62f1bf064568aa5b5c854484e9da",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Map = geemap.Map(center=[40,-100], zoom=4)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add Earth Engine Python script "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n",
      "Hello World from Earth Engine!\n",
      "{'type': 'Image', 'bands': [{'id': 'B1', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B2', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B3', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B4', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B5', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B6', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B7', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B8', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [15321, 15601], 'crs': 'EPSG:32610', 'crs_transform': [15, 0, 460792.5, 0, -15, 4264207.5]}, {'id': 'B9', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B10', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'B11', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}, {'id': 'BQA', 'data_type': {'type': 'PixelType', 'precision': 'int', 'min': 0, 'max': 65535}, 'dimensions': [7661, 7801], 'crs': 'EPSG:32610', 'crs_transform': [30, 0, 460785, 0, -30, 4264215]}], 'id': 'LANDSAT/LC08/C01/T1/LC08_044034_20140318', 'version': 1497472259022000.0, 'properties': {'RADIANCE_MULT_BAND_5': 0.006170900072902441, 'RADIANCE_MULT_BAND_6': 0.001534600043669343, 'RADIANCE_MULT_BAND_3': 0.011958000250160694, 'RADIANCE_MULT_BAND_4': 0.010084000416100025, 'RADIANCE_MULT_BAND_1': 0.012672999873757362, 'RADIANCE_MULT_BAND_2': 0.012977000325918198, 'K2_CONSTANT_BAND_11': 1201.1441650390625, 'K2_CONSTANT_BAND_10': 1321.078857421875, 'system:footprint': {'type': 'LinearRing', 'coordinates': [[-121.3637119499993, 36.41016684133052], [-121.35905784815819, 36.42528989660049], [-121.2315833015866, 36.840374852891664], [-121.09978718573184, 37.26438246506325], [-121.00571062336425, 37.564795515259384], [-120.98453376062118, 37.632161601008896], [-120.95100979452299, 37.73864548098522], [-120.90277241165228, 37.89149086576169], [-120.8836409072059, 37.951976016520376], [-120.85713152433351, 38.03584247073611], [-120.82804345546616, 38.12789513604401], [-122.38148159443172, 38.42337450676813], [-122.9500220192271, 38.525813632077686], [-122.95103687833704, 38.52422133103557], [-122.9569591344694, 38.504384836247866], [-123.43853932998316, 36.805122381748035], [-123.18722447462653, 36.759167415189125], [-121.5105534682754, 36.43765126135182], [-121.36447385999617, 36.408418528930035], [-121.3637119499993, 36.41016684133052]]}, 'REFLECTIVE_SAMPLES': 7661, 'SUN_AZIMUTH': 146.2395782470703, 'CPF_NAME': 'LC08CPF_20140101_20140331_01.01', 'DATE_ACQUIRED': '2014-03-18', 'ELLIPSOID': 'WGS84', 'google:registration_offset_x': 0, 'google:registration_offset_y': 0, 'STATION_ID': 'LGN', 'RESAMPLING_OPTION': 'CUBIC_CONVOLUTION', 'ORIENTATION': 'NORTH_UP', 'WRS_ROW': 34, 'RADIANCE_MULT_BAND_9': 0.0024117000866681337, 'TARGET_WRS_ROW': 34, 'RADIANCE_MULT_BAND_7': 0.0005172499804757535, 'RADIANCE_MULT_BAND_8': 0.0114120002835989, 'IMAGE_QUALITY_TIRS': 9, 'TRUNCATION_OLI': 'UPPER', 'CLOUD_COVER': 0.05999999865889549, 'GEOMETRIC_RMSE_VERIFY': 3.249000072479248, 'COLLECTION_CATEGORY': 'T1', 'GRID_CELL_SIZE_REFLECTIVE': 30, 'CLOUD_COVER_LAND': 0.10000000149011612, 'GEOMETRIC_RMSE_MODEL': 6.78000020980835, 'COLLECTION_NUMBER': 1, 'IMAGE_QUALITY_OLI': 9, 'LANDSAT_SCENE_ID': 'LC80440342014077LGN01', 'WRS_PATH': 44, 'google:registration_count': 0, 'PANCHROMATIC_SAMPLES': 15321, 'PANCHROMATIC_LINES': 15601, 'GEOMETRIC_RMSE_MODEL_Y': 4.747000217437744, 'REFLECTIVE_LINES': 7801, 'TIRS_STRAY_LIGHT_CORRECTION_SOURCE': 'TIRS', 'GEOMETRIC_RMSE_MODEL_X': 4.841000080108643, 'system:asset_size': 1105511852, 'system:index': 'LC08_044034_20140318', 'REFLECTANCE_ADD_BAND_1': -0.10000000149011612, 'REFLECTANCE_ADD_BAND_2': -0.10000000149011612, 'DATUM': 'WGS84', 'REFLECTANCE_ADD_BAND_3': -0.10000000149011612, 'REFLECTANCE_ADD_BAND_4': -0.10000000149011612, 'RLUT_FILE_NAME': 'LC08RLUT_20130211_20150302_01_11.h5', 'REFLECTANCE_ADD_BAND_5': -0.10000000149011612, 'REFLECTANCE_ADD_BAND_6': -0.10000000149011612, 'REFLECTANCE_ADD_BAND_7': -0.10000000149011612, 'REFLECTANCE_ADD_BAND_8': -0.10000000149011612, 'BPF_NAME_TIRS': 'LT8BPF20140318182855_20140318190505.01', 'GROUND_CONTROL_POINTS_VERSION': 4, 'DATA_TYPE': 'L1TP', 'UTM_ZONE': 10, 'LANDSAT_PRODUCT_ID': 'LC08_L1TP_044034_20140318_20170307_01_T1', 'REFLECTANCE_ADD_BAND_9': -0.10000000149011612, 'google:registration_ratio': 0, 'GRID_CELL_SIZE_PANCHROMATIC': 15, 'RADIANCE_ADD_BAND_4': -50.419559478759766, 'REFLECTANCE_MULT_BAND_7': 1.9999999494757503e-05, 'system:time_start': 1395168392050, 'RADIANCE_ADD_BAND_5': -30.854249954223633, 'REFLECTANCE_MULT_BAND_6': 1.9999999494757503e-05, 'RADIANCE_ADD_BAND_6': -7.67317008972168, 'REFLECTANCE_MULT_BAND_9': 1.9999999494757503e-05, 'PROCESSING_SOFTWARE_VERSION': 'LPGS_2.7.0', 'RADIANCE_ADD_BAND_7': -2.5862700939178467, 'REFLECTANCE_MULT_BAND_8': 1.9999999494757503e-05, 'RADIANCE_ADD_BAND_1': -63.364051818847656, 'RADIANCE_ADD_BAND_2': -64.88555908203125, 'RADIANCE_ADD_BAND_3': -59.79148864746094, 'REFLECTANCE_MULT_BAND_1': 1.9999999494757503e-05, 'RADIANCE_ADD_BAND_8': -57.06106185913086, 'REFLECTANCE_MULT_BAND_3': 1.9999999494757503e-05, 'RADIANCE_ADD_BAND_9': -12.058540344238281, 'REFLECTANCE_MULT_BAND_2': 1.9999999494757503e-05, 'REFLECTANCE_MULT_BAND_5': 1.9999999494757503e-05, 'REFLECTANCE_MULT_BAND_4': 1.9999999494757503e-05, 'THERMAL_LINES': 7801, 'TIRS_SSM_POSITION_STATUS': 'NOMINAL', 'GRID_CELL_SIZE_THERMAL': 30, 'NADIR_OFFNADIR': 'NADIR', 'RADIANCE_ADD_BAND_11': 0.10000000149011612, 'REQUEST_ID': '0501703063989_00019', 'EARTH_SUN_DISTANCE': 0.9953709244728088, 'TIRS_SSM_MODEL': 'ACTUAL', 'FILE_DATE': 1488849349000, 'SCENE_CENTER_TIME': '18:46:32.0535800Z', 'SUN_ELEVATION': 46.471065521240234, 'BPF_NAME_OLI': 'LO8BPF20140318183249_20140318190412.01', 'RADIANCE_ADD_BAND_10': 0.10000000149011612, 'ROLL_ANGLE': -0.0010000000474974513, 'K1_CONSTANT_BAND_10': 774.8853149414062, 'SATURATION_BAND_1': 'Y', 'SATURATION_BAND_2': 'Y', 'SATURATION_BAND_3': 'Y', 'SATURATION_BAND_4': 'Y', 'SATURATION_BAND_5': 'Y', 'MAP_PROJECTION': 'UTM', 'SATURATION_BAND_6': 'Y', 'SENSOR_ID': 'OLI_TIRS', 'SATURATION_BAND_7': 'Y', 'K1_CONSTANT_BAND_11': 480.8883056640625, 'SATURATION_BAND_8': 'N', 'SATURATION_BAND_9': 'N', 'TARGET_WRS_PATH': 44, 'RADIANCE_MULT_BAND_11': 0.00033420001273043454, 'RADIANCE_MULT_BAND_10': 0.00033420001273043454, 'GROUND_CONTROL_POINTS_MODEL': 527, 'SPACECRAFT_ID': 'LANDSAT_8', 'ELEVATION_SOURCE': 'GLS2000', 'THERMAL_SAMPLES': 7661, 'GROUND_CONTROL_POINTS_VERIFY': 164}}\n"
     ]
    }
   ],
   "source": [
    "# Add Earth Engine dataset\n",
    "# traditional python string\n",
    "print('Hello world!')\n",
    "\n",
    "# Earth Eninge object\n",
    "print(ee.String('Hello World from Earth Engine!').getInfo())\n",
    "print(ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318').getInfo())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display Earth Engine data layers "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayerControl() # This line is not needed for ipyleaflet-based Map.\n",
    "Map"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Table of Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
